<?php
/*
** @introduction: 分类操作类
** @author: 杨陈鹏
** @date: 2015-03-25 20:06
** @email: yangchenpeng@cdlinglu.com
*/

namespace Admin\Controller\Goods;


use Common\Controller\Admin\CommonController;

class CategoryController extends CommonController
{
    /*
     ** @introduction: 分类列表
     ** @author: 杨陈鹏
     ** @date: 2015-04-50 03:50
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function lists()
    {

        $this->pid = I('post.pid') ? I('post.pid') : 0;

        $logic = D('Common/Category', 'Logic');

        $data = $logic::getData(['pid' => $this->pid]);

        if ($this->pid) {

            $this->ppid = $logic::$model->where(['id' => $this->pid])->getField('pid');

        }

        $this->data = $data['data'];

        $this->display();

    }

    /*
     ** @introduction: 创建分类
     ** @author: 杨陈鹏
     ** @date: 2015-04-50 03:50
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function create()
    {


        $data = I('post.data');

        $logic = D('Common/Category', 'Logic');

        if ($data) {

            $this->ajaxReturn($logic::create($data));

        } else {
            //获取所有规则
            $categorys = $logic::getChilds(0, 'id,name,pid');

            //获取树型下拉框
            $tree = new \Org\Util\Tree();

            $tree->init($categorys);

            $this->category_tree = $tree->create(0, '<option value=\'$id\'>$spacer $name</option >');

            $this->display();

        }

    }

    /*
     ** @introduction: 分类排序
     ** @author: 杨陈鹏
     ** @date: 2015-04-8 11:37
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function sort()
    {

        $logic = D('Common/Category', 'Logic');
        $sorts = I('post.data');

        if (!$sorts)
            $this->ajaxReturn(['code' => 300, 'msg' => '参数错误!']);

        foreach ($sorts as $k => $v) {

            $logic::$model->where(['id' => $k])->setField(['sort' => intval($v)]);

        }

        $this->ajaxReturn(['code' => 200, 'msg' => '排序成功']);

    }

    /*
     ** @introduction: 删除分类
     ** @author: 杨陈鹏
     ** @date: 2015-04-8 11:37
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function delete()
    {

        $id = I('post.data');

        if (!$id)
            $this->ajaxReturn(['code' => 300, 'msg' => '参数错误!']);

        $logic = D('Common/Category', 'Logic');

        $this->ajaxReturn($logic::delete($id));

    }

    /*
     ** @introduction: 更新数据
     ** @author: 杨陈鹏
     ** @date: 2015-04-8 11:37
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function update()
    {

        $data = I('post.data');

        $logic = D('Common/Category', 'Logic');

        if (isset($data['opid'])) {

            $this->ajaxReturn($logic::update($data));

        } else {

            $this->id = $data['id'];

            //获取所有规则
            $categorys = $logic::getChilds(0, 'id,name,pid');

            //获取当前分类数据
            $this->data = $logic::getOneData(['id' => $this->id]);

            $this->ppid = $this->data['pid'];

            //获取树型下拉框
            $tree = new \Org\Util\Tree();

            $tree->init($categorys);

            $this->category_tree = $tree->create(0, "<option value=\$id \$selected \$disabled>\$spacer \$name</option>", $this->data['pid'], '', false, $data['id']);


            $this->display();

        }

    }
}